package com.naiterui.ehp.bp.security.controller;

import com.naiterui.ehp.bp.security.service.IOnlineUserService;
import com.naiterui.ehp.bp.security.utils.EncryptUtils;
import com.naiterui.ehp.bp.security.vo.OnlineUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 在线用户管理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/auth/online")
@Api(tags = "系统：在线用户管理")
public class OnlineController {

  private final IOnlineUserService onlineUserService;

  @ApiOperation("查询在线用户")
  @GetMapping
  @PreAuthorize("@p.check()")
  public List<OnlineUserVO> query(String filter) {
    return this.onlineUserService.getAll(filter);
  }

  @ApiOperation("踢出用户")
  @DeleteMapping
  @PreAuthorize("@p.check()")
  public void delete(@RequestBody Set<String> keys) {
    keys.stream().map(EncryptUtils::desDecrypt).forEach(this.onlineUserService::kickOut);
  }

}
